package org.jianghu.activity.main.view;

import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;

import org.jianghu.R;
import org.jianghu.activity.main.presenter.MainPresenterCompl;
import org.jianghu.activity.user.login.view.LoginActivity;
import org.jianghu.base.BaseActivity;
import org.jianghu.fragment.testhome.FragmentTestHome;
import org.jianghu.fragment.testone.Fragment_one;
import org.jianghu.fragment.testthree.Fragment_three;
import org.jianghu.fragment.testtwo.Fragment_two;
import org.jianghu.utils.IntentUtil;
import org.jianghu.view.CircleImageView;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;


@ContentView(R.layout.activity_main)
public class MainActivity extends BaseActivity implements MainView {

    private final static int TAB_ONE = 0;
    private final static int TAB_TWO = 1;
    private final static int TAB_THREE = 2;
    private final static int TAB_FRO = 3;

    private boolean isLogin = false;//是否登陆

    private MainPresenterCompl mainPressenterComple;

    private FragmentManager fragmentManager;

    private ActionBarDrawerToggle mDrawerToggle;
    @ViewInject(R.id.main_toolbar)
    Toolbar toolbar;

    @ViewInject(R.id.bottom_navigation_bar)
    private BottomNavigationBar bottomNavigationBar;

    @ViewInject(R.id.main_navigation)
    private NavigationView navigationView;

    @ViewInject(R.id.main_drawerLayout)
    private DrawerLayout mDrawerLayout;

    private TextView user_name;//用户昵称
    private CircleImageView user_photo;//用户头像

    FragmentTestHome fragment_home;
    Fragment_one fragment_one;
    Fragment_two fragment_two;
    Fragment_three fragment_three;

    @Override
    public void initViewsAndValues(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        x.view().inject(this);
        initToolbar();//初始化ToolBar
        initBottomBar();//初始化底部标签

        mainPressenterComple.isLogin();//判断是否登陆
    }

    @Override
    public void initParmers() {

        mainPressenterComple = new MainPresenterCompl(this, this);

        fragmentManager = getSupportFragmentManager();

    }

    @Override
    public void releaseOnDestory() {

    }

    /*
        初始化Toolbar
     */
    private void initToolbar() {
        toolbar.inflateMenu(R.menu.base_topbar_menu);

        toolbar.setNavigationIcon(R.mipmap.topbar_menu_home);

        //绑定侧拉栏和ToolBar
        mDrawerToggle = new ActionBarDrawerToggle(MainActivity.this, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);

        toolbar.setTitle(R.string.app_name);
        toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));

//        setSupportActionBar(toolbar);

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                int itemId = item.getItemId();
                switch (itemId) {
                    case R.id.action_notification:
                        bottomNavigationBar.unHide(true);
                        break;
                }
                return true;
            }
        });

        //获取到侧拉栏的头部View
        View headerView = navigationView.getHeaderView(0);
        user_name = (TextView) headerView.findViewById(R.id.user_name);
        user_photo = (CircleImageView) headerView.findViewById(R.id.user_photo);
        user_photo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (isLogin) {
                    showFragment(TAB_FRO);
                } else {
                    IntentUtil.intentDIY(MainActivity.this, LoginActivity.class);
                }
            }
        });
    }

    /*
       初始化底部标签
    */
    private void initBottomBar() {

        bottomNavigationBar.setActiveColor(R.color.color_cd3e3a);//设置标签背景颜色
        bottomNavigationBar.setInActiveColor(R.color.color_005eaa);//设置标签未选中时颜色
        bottomNavigationBar.setBarBackgroundColor(R.color.color_f5f5f5);//设置标签选中背景颜色

        /*
            MODE_FIXED:填充模式，未选中的Item会显示文字，没有换挡动画。
            MODE_SHIFTING:换挡模式，未选中的Item不会显示文字，选中的会显示文字。在切换的时候会有一个像换挡的动画
         */
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);

        /*BACKGROUND_STYLE_STATIC:点击的时候没有水波纹效果
          BACKGROUND_STYLE_RIPPLE:点击的时候有水波纹效果
         */
        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_STATIC);

//        BadgeItem badge = new BadgeItem();
//        badge.setBorderWidth(2);
//        badge.setBorderColor(R.color.color_f5f5f5);
//        badge.setBackgroundColor(R.color.color_e81d62);
//        badge.setGravity(Gravity.RIGHT | Gravity.TOP);
//        badge.setText("5");
//        badge.setHideOnSelect(true);

        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.icon_home, R.string.tab_home))
                .addItem(new BottomNavigationItem(R.mipmap.icon_find, R.string.tab_find))
                .addItem(new BottomNavigationItem(R.mipmap.icon_wright, R.string.tab_wright))
                .addItem(new BottomNavigationItem(R.mipmap.icon_message, R.string.tab_message)).initialise();

        //标签点击事件
        bottomNavigationBar.setTabSelectedListener(new BottomNavigationBar.OnTabSelectedListener() {
            @Override
            public void onTabSelected(int position) {//未选中 -> 选中
                showFragment(position);
            }

            @Override
            public void onTabUnselected(int position) {//选中 -> 未选中

            }

            @Override
            public void onTabReselected(int position) {//选中 -> 选中

            }
        });

        showFragment(TAB_ONE);
    }

    /*
        Fragment跳转判断
     */

    private void showFragment(int frag) {
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideAllFragment(transaction);

        switch (frag) {
            case TAB_ONE:
                if (fragment_home != null) {
                    transaction.show(fragment_home);
                } else {
                    fragment_home = new FragmentTestHome();
                    transaction.add(R.id.fragment_frame_layout, fragment_home);
                }
                toolbar.setTitle("首页");
                break;

            case TAB_TWO:
                if (fragment_one != null) {
                    transaction.show(fragment_one);
                } else {
                    fragment_one = new Fragment_one();
                    transaction.add(R.id.fragment_frame_layout, fragment_one);
                }

                toolbar.setTitle("收件");
                break;

            case TAB_THREE:
                if (fragment_two != null) {
                    transaction.show(fragment_two);
                } else {
                    fragment_two = new Fragment_two();
                    transaction.add(R.id.fragment_frame_layout, fragment_two);
                }
                toolbar.setTitle("发件");

                bottomNavigationBar.hide(true);
                break;

            case TAB_FRO:

                IntentUtil.intentDIY(MainActivity.this, LoginActivity.class);

                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideAllFragment(FragmentTransaction ft) {
        if (fragment_home != null) {
            ft.hide(fragment_home);
        }
        if (fragment_one != null) {
            ft.hide(fragment_one);
        }
        if (fragment_two != null) {
            ft.hide(fragment_two);
        }
        if (fragment_three != null) {
            ft.hide(fragment_three);
        }
    }

    @Override
    public void isLogin(boolean isLogin) {
        if (isLogin) {
            isLogin = true;
        } else {
            isLogin = false;
            user_name.setText(R.string.user_name);
        }
    }
}
